package com.ldoublem.loadingviewlib;



import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import com.ldoublem.loadingviewlib.utils.ArgbEvaluator;



public class LVChromeLogo extends Component implements Component.EstimateSizeListener, Component.DrawTask {

    private Paint mPaintRed, mPaintYellow, mPaintGreen, mPaintBulue, mPaintWhite, mPaintLine;

    private float mWidth = 0f;
    private float mPadding = 0f;


    ArgbEvaluator evaluator;
    private int endColor = Color.rgb(0, 0, 0);
    private int startYellowColor = Color.argb(100, 253, 197, 53);

    private int startGreenColor = Color.argb(100, 27, 147, 76);
    private int startRedColor = Color.argb(100, 211, 57, 53);


    private int lineColor;

    AnimatorProperty mProgerssRotateAnim;

    public LVChromeLogo(Context context) {
        this(context, null);
    }

    public LVChromeLogo(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public LVChromeLogo(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void drawSector(Canvas canvas) {
        RectFloat rectF = new RectFloat(mPadding, mPadding,
                mWidth - mPadding, mWidth - mPadding);
        canvas.drawArc(rectF, new Arc(-30, 120
                , true), mPaintYellow);
        canvas.drawArc(rectF, new Arc(90, 120
                , true), mPaintGreen);
        canvas.drawArc(rectF, new Arc(210, 120
                , true), mPaintRed);
    }


    private void drawTriangle(Canvas canvas) {//画三个等边三角形组成的大三角形正好是内切三角形
        Point point1 = getPoint((mWidth / 2 - mPadding) / 2, 90);
        Point point2 = getPoint((mWidth / 2 - mPadding), 150);
        Point point3 = getPoint((mWidth / 2 - mPadding) / 2, 210);
        Point point4 = getPoint((mWidth / 2 - mPadding), 270);
        Point point5 = getPoint((mWidth / 2 - mPadding) / 2, 330);
        Point point6 = getPoint((mWidth / 2 - mPadding), 30);
        Path pathYellow = new Path();
        pathYellow.moveTo(mWidth / 2 - point1.x, mWidth / 2 - point1.y);
        pathYellow.lineTo(mWidth / 2 - point2.x, mWidth / 2 - point2.y);
        pathYellow.lineTo(mWidth / 2 - point3.x, mWidth / 2 - point3.y);
        pathYellow.close();
        Path pathGreen = new Path();
        pathGreen.moveTo(mWidth / 2 - point3.x, mWidth / 2 - point3.y);
        pathGreen.lineTo(mWidth / 2 - point4.x, mWidth / 2 - point4.y);
        pathGreen.lineTo(mWidth / 2 - point5.x, mWidth / 2 - point5.y);
        pathGreen.close();
        Path pathRed = new Path();
        pathRed.moveTo(mWidth / 2 - point5.x, mWidth / 2 - point5.y);
        pathRed.lineTo(mWidth / 2 - point6.x, mWidth / 2 - point6.y);
        pathRed.lineTo(mWidth / 2 - point1.x, mWidth / 2 - point1.y);
        pathRed.close();
        canvas.drawPath(pathGreen, mPaintGreen);
        canvas.drawPath(pathRed, mPaintRed);
        canvas.drawPath(pathYellow, mPaintYellow);

        //扇形交接处隐形效果
        for (int i = 0; i < Math.abs(mWidth / 2 - point2.y) / 2f; i++) {

            int fraction = 35 - i;
            if (fraction > 0) {
                lineColor = (Integer) evaluator.evaluate(fraction / 100f, startYellowColor, endColor);
                mPaintLine.setColor(new Color(lineColor));
            } else {
                mPaintLine.setColor(new Color(Color.argb(0, 0, 0, 0)));
            }

            canvas.drawLine(
                    new ohos.agp.utils.Point(mWidth / 2, point2.y + i),
                    new ohos.agp.utils.Point(mWidth / 2 - point2.x * 8f / 10f, mWidth / 2 - point2.y),
                    mPaintLine);

        }


        for (int i = 0; i < Math.abs(point3.x) / 2f; i++) {
            int fraction = 35 - i;
            if (fraction > 0) {
                lineColor = (Integer) evaluator.evaluate(fraction / 100f, startGreenColor, endColor);
                mPaintLine.setColor(new Color(lineColor));
            } else

                mPaintLine.setColor(new Color(Color.argb(0, 0, 0, 0)));

            canvas.drawLine(
                    new ohos.agp.utils.Point(mWidth / 2 - point3.x - i, mWidth / 2 - point3.y),
                    new ohos.agp.utils.Point(mWidth / 2 - point4.x, mWidth / 2 - point4.y),
                    mPaintLine);


        }


        for (int i = 0; i < Math.abs(mWidth / 2 - point5.x) / 2f; i++) {
            int fraction = 30 - i;
            if (fraction > 0) {
                lineColor = (Integer) evaluator.evaluate(fraction / 100f, startRedColor, endColor);
                mPaintLine.setColor(new Color(lineColor));
            } else

                mPaintLine.setColor(new Color(Color.argb(0, 0, 0, 0)));
            canvas.drawLine(new ohos.agp.utils.Point(mWidth / 2 - point5.x + i, mWidth / 2 - point5.y),
                    new ohos.agp.utils.Point(mWidth / 2 - point6.x, mWidth / 2 - point6.y),
                    mPaintLine);

        }

    }


    private void drawCircle(Canvas canvas) {//画中心的圆覆盖
        canvas.drawCircle(mWidth / 2, mWidth / 2, (mWidth / 2 - mPadding) / 2, mPaintWhite);
        canvas.drawCircle(mWidth / 2, mWidth / 2, (mWidth / 2 - mPadding) / 2 / 6 * 5, mPaintBulue);
    }






    public void startAnim() {
        stopAnim();
        mProgerssRotateAnim.setDuration(1500);
        mProgerssRotateAnim.start();
    }
    public void startAnim(int time) {
        stopAnim();
        mProgerssRotateAnim.setDuration(time);

        mProgerssRotateAnim.start();

    }
    public void stopAnim() {
        mProgerssRotateAnim.cancel();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.save();
        drawSector(canvas);
        drawTriangle(canvas);
        drawCircle(canvas);
        canvas.restore();
    }

    private class Point {
        private float x;
        private float y;

        private Point(float x, float y) {
            this.x = x;
            this.y = y;
        }


    }

    private Point getPoint(float radius, float angle) {
        float x = (float) ((radius) * Math.cos(angle * Math.PI / 180f));
        float y = (float) ((radius) * Math.sin(angle * Math.PI / 180f));
        Point p = new Point(x, y);
        return p;
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {

        if (EstimateSpec.getSize(i) > getHeight())
            mWidth = EstimateSpec.getSize(i1);
        else
            mWidth = EstimateSpec.getSize(i);


        mPadding = dip2px(1);
        return false;
    }


    private void initPaint() {
        setEstimateSizeListener(this);
        addDrawTask(this);

        evaluator = new ArgbEvaluator();

        mPaintRed = new Paint();
        mPaintRed.setAntiAlias(true);
        mPaintRed.setStyle(Paint.Style.FILL_STYLE);
        mPaintRed.setColor(new Color(Color.rgb(211, 57, 53)));


        mPaintYellow = new Paint();
        mPaintYellow.setAntiAlias(true);
        mPaintYellow.setStyle(Paint.Style.FILL_STYLE);
        mPaintYellow.setColor(new Color(Color.rgb(253, 197, 53)));


        mPaintGreen = new Paint();
        mPaintGreen.setAntiAlias(true);
        mPaintGreen.setStyle(Paint.Style.FILL_STYLE);
        mPaintGreen.setColor(new Color(Color.rgb(27, 147, 76)));


        mPaintBulue = new Paint();
        mPaintBulue.setAntiAlias(true);
        mPaintBulue.setStyle(Paint.Style.FILL_STYLE);
        mPaintBulue.setColor(new Color((Color.rgb(61, 117, 242))));

        mPaintWhite = new Paint();
        mPaintWhite.setAntiAlias(true);
        mPaintWhite.setStyle(Paint.Style.FILL_STYLE);
        mPaintWhite.setColor(Color.WHITE);


        mPaintLine = new Paint();
        mPaintLine.setAntiAlias(true);
        mPaintLine.setStyle(Paint.Style.FILL_STYLE);
        mPaintLine.setColor(new Color(Color.argb(30, 0, 0, 0)));


        mProgerssRotateAnim = this.createAnimatorProperty();
        mProgerssRotateAnim.rotate(360);
        mProgerssRotateAnim.setLoopedCount(-1);



    }

    public int dip2px(float dpValue) {
        final float scale = AttrHelper.getDensity(getContext());
        return (int) (dpValue * scale + 0.5f);
    }
}
